
package com.ezlcp.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ezlcp.commons.base.db.BaseDao;
import com.ezlcp.commons.base.db.BaseService;
import com.ezlcp.commons.service.impl.SuperServiceImpl;
import com.ezlcp.commons.utils.ContextUtil;
import com.ezlcp.user.entity.DicValue;
import com.ezlcp.user.entity.Dictionary;
import com.ezlcp.user.mapper.DictionaryMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * [数据字典]业务服务类
 */
@Service
public class DictionaryServiceImpl extends SuperServiceImpl<DictionaryMapper, Dictionary> implements BaseService<Dictionary> {
    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Resource
    private DicValueServiceImpl dicValueService;

    @Override
    public BaseDao<Dictionary> getRepository() {
        return dictionaryMapper;
    }

    /***
     * @description: 根据当前用户角色查询数据字典
     * @return java.util.List<com.hkyctech.loan.core.entity.Dictionary> 符合角色要求字典列表
     * @author Weixuan LONG
     * @date 2022/5/11 10:12
     */
    public List<Dictionary> getByDicKey(String dicKeyList) {
        String[] dicKey = dicKeyList.split(",");
        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.and(query -> query.eq("is_fixed", 1).or().eq("tenant_id", ContextUtil.getCurrentTenantId())).in("dic_key", dicKey);
        List<Dictionary> dictionaryList = dictionaryMapper.selectList(wrapper);
        // 查询子项列表
        dictionaryList.forEach(dictionary -> {
            List<DicValue> dicValueList = dicValueService.getByDicId(dictionary.getDicId());
            dictionary.setDicValueList(dicValueList);
        });
        return dictionaryList;
    }
}